
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var helperOpen = require("helper/open");
var http = require("helper/http");
var mallUrl = require("url/mall");

var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据

var noData = [{"template":1,"labelContent":"店铺没有该商品"}];
var addShare = ui("$").add("share","source://view/reuse/share.ui", 0, 0);
//当前页码
var pageNum = 1;

/* 头部 */
var headerLeft=ui("headerLeft");
var do_ALayout_search=ui("do_ALayout_search");

var isChangeList = false;
headerLeft.on("touch", function() {
	app.closePage({mall_product_chage: isChangeList});
});
var productName=null;
do_ALayout_search.on("touch", function() {
	productName=ui("do_TextField_search").text;
	loadFirstProductPage();
});
do_ALayout_search.on("enter", function(data, e) {
	productName=ui("do_TextField_search").text;
	loadFirstProductPage();
});
//andriod返回键
page.on("back", function() {
	app.closePage({mall_product_chage: isChangeList});
});
var shopId = null;
page.on("loaded", function(){
	shopId = page.getData().shopId;
	loadFirstProductPage();
});

page.on("result", function(data){
	if(data.mall_edit_product){
		loadFirstProductPage();
		isChangeList = true;
	}
});

page.on("mall_on_shelf_product", function(data, e) {
	loadFirstProductPage();
	isChangeList = true;
})


//加载商品--第一页
function loadFirstProductPage() {
	if(shopId== null){
		return;
	}
	pageNum=1;
	http.post(mallUrl.url.product.listOnShelfProducts4MyShop, {shopId: shopId, page: pageNum,name:productName}, function(data) {
		var productPage = data.productPage;
		if(productPage){
			if(listdata.getCount()>0){
				listdata.removeAll();
			}
			if(productPage.content.length){
				listdata.addData(productPage.content);//加载商品列
			}else{
				listdata.addData(noData);//没有商品
			}
			listview.refreshItems();
		}		
	});
}
//加载商品--下一页
function loadNextProductPage() {
	if(shopId== null){
		return;
	}
	pageNum ++;
	http.post(mallUrl.url.product.listOnShelfProducts4MyShop, {shopId: shopId, page: pageNum,name:productName}, function(data) {
		var productPage = data.productPage;
		if(productPage){
			if(productPage.content.length){
				listdata.addData(productPage.content);//加载商品列
				listview.refreshItems();
			}
		}		
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		loadFirstProductPage();
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		loadNextProductPage();
		listview.rebound();//复位【一定要做】
	}
});
